package com.ruoyi.cm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.base.domain.BaseBsBd;
import com.ruoyi.base.domain.BaseBsFbfx;
import com.ruoyi.base.domain.BaseBsProject;
import com.ruoyi.base.domain.BaseBsZd;
import com.ruoyi.base.domain.bo.BaseBsBdBo;
import com.ruoyi.base.domain.vo.BaseBsBdVo;
import com.ruoyi.base.domain.vo.BaseBsFbfxVo;
import com.ruoyi.base.domain.vo.BaseBsZdVo;
import com.ruoyi.base.domain.vo.BdTreeVo;
import com.ruoyi.base.mapper.BaseBsBdMapper;
import com.ruoyi.base.mapper.BaseBsFbfxMapper;
import com.ruoyi.base.mapper.BaseBsProjectMapper;
import com.ruoyi.base.mapper.BaseBsZdMapper;
import com.ruoyi.base.service.IBaseBsBdService;
import com.ruoyi.cm.domain.CmBsProcess;
import com.ruoyi.cm.domain.CmBsProcessInspectionInfo;
import com.ruoyi.cm.mapper.CmBsProcessInspectionInfoMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.flowable.core.domain.ProcessQuery;
import com.ruoyi.system.domain.SysUserPower;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysUserPowerMapper;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.workflow.domain.vo.WfTaskVo;
import com.ruoyi.workflow.service.IWfProcessService;
import com.ruoyi.workflow.service.IWfTaskService;
import lombok.RequiredArgsConstructor;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Service;
import com.ruoyi.cm.domain.bo.CmBsProcessInspectionBo;
import com.ruoyi.cm.domain.vo.CmBsProcessInspectionVo;
import com.ruoyi.cm.domain.CmBsProcessInspection;
import com.ruoyi.cm.mapper.CmBsProcessInspectionMapper;
import com.ruoyi.cm.service.ICmBsProcessInspectionService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 工序报验Service业务层处理
 *
 * @author zxs
 * @date 2024-10-16
 */
@RequiredArgsConstructor
@Service
public class CmBsProcessInspectionServiceImpl implements ICmBsProcessInspectionService {

    private final CmBsProcessInspectionMapper baseMapper;
    private final CmBsProcessInspectionInfoMapper inspectionInfoMapper;
    private final ISysOssService sysOssService;
    private final BaseBsFbfxMapper fbfxMapper;
    private final BaseBsZdMapper zdMapper;
    private final BaseBsBdMapper bdMapper;
    private final BaseBsProjectMapper projectMapper;
    private final SysUserPowerMapper userPowerMapper;
    private final IBaseBsBdService bdService;
    private final IWfProcessService processService;
    private final TaskService taskService;


    /**
     * 查询工序报验
     */
    @Override
    public CmBsProcessInspectionVo queryById(Long id) {
        CmBsProcessInspectionVo vo = baseMapper.selectVoById(id);
        // 标段项目信息
        if (vo.getZdId() != null) {
            BaseBsZd zd = zdMapper.selectById(vo.getZdId());

            if (zd != null) {
                vo.setZdName(zd.getName());
                BaseBsBd bd = bdMapper.selectById(zd.getBdId());

                if (bd != null) {
                    vo.setBdId(bd.getId());
                    vo.setBdName(bd.getName());

                    BaseBsProject project = projectMapper.selectById(bd.getProjectId());
                    if (project != null) {
                        vo.setProjectId(project.getProjectId());
                        vo.setProjectName(project.getProjectName());
                    }
                }

            }
        }

        // 审批信息
        if (ObjectUtils.isNotEmpty(vo.getProcInsId())) {
            Task task = taskService.createTaskQuery().processInstanceId(vo.getProcInsId()).singleResult();
            if (task != null) {
                vo.setTaskId(task.getId());
                if (task.getAssignee() != null && (Long.valueOf(task.getAssignee()) == LoginHelper.getUserId())) {
                    vo.setCurrApproval(true);
                } else {
                    vo.setCurrApproval(false);
                }
            }
        }
        //查询子表；
        List<CmBsProcessInspectionInfo> voList =
            inspectionInfoMapper.selectList(new LambdaQueryWrapper<CmBsProcessInspectionInfo>().eq(CmBsProcessInspectionInfo::getProcessInspectionId, id));
        for (CmBsProcessInspectionInfo info : voList) {
            String fileNo = info.getFileNo();
            if (StringUtils.isNotBlank(fileNo)) {
                info.setSysOssVos(getfilesByFileIds(fileNo));
            }
        }
        vo.setInfoList(voList);
        //查询主表附件列表
        String fileNo = vo.getFileNo();
        if (StringUtils.isNotBlank(fileNo)) {
            vo.setSysOssVos(getfilesByFileIds(fileNo));
        }
        //拼接所有父级分部分项名称；
        vo.setFbfxName(getFbfxNamepj(vo.getFbfxId(), true));
        return vo;
    }

    /**
     * 分部分项结构拼接
     * @param fbfxId
     * @param isZd 是否带有站点名称
     * @return
     */
    private String getFbfxNamepj(Long fbfxId, boolean isZd) {
        String fbfxName = "";
        BaseBsFbfxVo fbfxVo = fbfxMapper.selectVoById(fbfxId);
        if(fbfxVo!=null) {
            List<BaseBsFbfxVo> fbfxVos = new ArrayList<>();
            fbfxVos.add(fbfxVo);
            BaseBsFbfxVo baseBsFbfxVo;
            Long parentId = fbfxVo.getParentId();
            BaseBsZdVo zdVo;
            for (; ; ) {
                baseBsFbfxVo = fbfxMapper.selectVoById(parentId);
                if (baseBsFbfxVo != null && baseBsFbfxVo.getParentId() != null) {
                    parentId = baseBsFbfxVo.getParentId();
                    fbfxVos.add(baseBsFbfxVo);
                } else {
                    //查询站点名称；
                    zdVo = zdMapper.selectVoById(parentId);
                    break;
                }
            }
            if (isZd && zdVo != null && zdVo.getName()!=null) {
                fbfxName = zdVo.getName() + "+";
            }
            for (int i = fbfxVos.size() - 1; i >= 0; i--) {
                BaseBsFbfxVo fbfx = fbfxVos.get(i);
                if (fbfx != null && StringUtils.isNotBlank(fbfx.getName())) {
                    fbfxName = fbfxName + fbfx.getName().trim() + "+";
                }
            }
        }
        return fbfxName.length() == 0 ? "" : fbfxName.substring(0, fbfxName.length() - 1);
    }

    //根据fileNo查询附件列表；
    public List<SysOssVo> getfilesByFileIds(String fileNo) {
        String[] split = fileNo.split(",");
        Set<Long> fileIds = new HashSet<>();
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            fileIds.add(Long.valueOf(s));
        }
        return sysOssService.listByIds(fileIds);
    }

    /**
     * 查询工序报验列表
     */
    @Override
    public TableDataInfo<CmBsProcessInspectionVo> queryPageList(CmBsProcessInspectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmBsProcessInspection> lqw = buildQueryWrapper(bo);
        Page<CmBsProcessInspectionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工序报验列表
     */
    @Override
    public List<CmBsProcessInspectionVo> queryList(CmBsProcessInspectionBo bo) {
        LambdaQueryWrapper<CmBsProcessInspection> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmBsProcessInspection> buildQueryWrapper(CmBsProcessInspectionBo bo) {
        Map<String, Object> params = bo.getParams();
        String beginTime ="";
        String endTime ="";
        LambdaQueryWrapper<CmBsProcessInspection> lqw = Wrappers.lambdaQuery();
        if(params.get("beginTime") != null && params.get("endTime") != null){
            beginTime = params.get("beginTime").toString();
            endTime = params.get("endTime").toString();
            lqw.between(CmBsProcessInspection::getInspectTime,beginTime,endTime);
        }
        lqw.like(StringUtils.isNotBlank(bo.getName()), CmBsProcessInspection::getName, bo.getName());
        lqw.eq(bo.getProcessId() != null, CmBsProcessInspection::getProcessId, bo.getProcessId());
        lqw.eq(bo.getFbfxId() != null, CmBsProcessInspection::getFbfxId, bo.getFbfxId());
        lqw.eq(bo.getQsId() != null, CmBsProcessInspection::getQsId, bo.getQsId());
        lqw.eq(StringUtils.isNotBlank(bo.getQs()), CmBsProcessInspection::getQs, bo.getQs());
        lqw.eq(StringUtils.isNotBlank(bo.getApproveStatus()), CmBsProcessInspection::getApproveStatus, bo.getApproveStatus());
        lqw.eq(bo.getInspectTime() != null, CmBsProcessInspection::getInspectTime, bo.getInspectTime());
        lqw.eq(bo.getSort() != null, CmBsProcessInspection::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getFileType()), CmBsProcessInspection::getFileType, bo.getFileType());
        lqw.eq(bo.getEnabled() != null, CmBsProcessInspection::getEnabled, bo.getEnabled());
        lqw.eq(bo.getCurrNodeName() != null, CmBsProcessInspection::getCurrNodeName, bo.getCurrNodeName());
        lqw.eq(bo.getProcInsId() != null, CmBsProcessInspection::getProcInsId, bo.getProcInsId());
        lqw.orderByAsc(CmBsProcessInspection::getSort);
        return lqw;
    }

    /**
     * 新增工序报验
     */
    @Override
    public Boolean insertByBo(CmBsProcessInspectionBo bo) {
        CmBsProcessInspection add = BeanUtil.toBean(bo, CmBsProcessInspection.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工序报验
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(CmBsProcessInspectionBo bo) {
        CmBsProcessInspection update = BeanUtil.toBean(bo, CmBsProcessInspection.class);
        validEntityBeforeSave(update);
        //先删除本次修改删掉的附件；
        Set<Long> fileNoOld = bo.getFileIdOld();
        Set<Long> fileNos = bo.getFileIds();
        Set<Long> deleteFiles;
        if(CollUtil.isNotEmpty(fileNoOld)){
            if(CollUtil.isEmpty(fileNos)){
                deleteFiles = fileNoOld;
            } else {
                deleteFiles = fileNoOld.stream().filter(e-> !fileNos.contains(e)).collect(Collectors.toSet());
            }
            if(CollUtil.isNotEmpty(deleteFiles)){
                sysOssService.deleteWithValidByIds(deleteFiles,true);
            }
        }
        baseMapper.updateById(update);
        //更新子表；
        List<CmBsProcessInspectionInfo> infoList = bo.getInfoList();
        if(CollUtil.isNotEmpty(infoList)) {
            //先删除本次修改删掉的附件；
            Set<Long> deleteFile = new HashSet<>();
            for (CmBsProcessInspectionInfo info : infoList) {
                Set<Long> fileIdOld = info.getFileIdOld();
                Set<Long> fileIds = info.getFileIds();
                if (CollUtil.isNotEmpty(fileIdOld)) {
                    if (CollUtil.isEmpty(fileNos)) {
                        deleteFile.addAll(fileIdOld);
                    } else {
                        deleteFile.addAll(fileIdOld.stream().filter(e -> !fileIds.contains(e)).collect(Collectors.toSet()));
                    }
                }
            }
            if (CollUtil.isNotEmpty(deleteFile)) {
                sysOssService.deleteWithValidByIds(deleteFile, true);
            }
            inspectionInfoMapper.updateBatchById(infoList);
        }
        return true;
    }

    /**
     * 批量删除工序报验
     */
    @Override
    @DSTransactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        //先删除工序报验子表
        inspectionInfoMapper.delete(new LambdaQueryWrapper<CmBsProcessInspectionInfo>().in(CmBsProcessInspectionInfo::getProcessInspectionId,ids));
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @DSTransactional
    public Boolean addBatch(CmBsProcessInspectionBo bo) {
        List<CmBsProcess> processList = bo.getProcessList();
        Long fbfxId = bo.getFbfxId();
        if (fbfxId == null || CollUtil.isEmpty(processList)) {
            return false;
        }
        List<CmBsProcessInspection> processInspections = new ArrayList<>();
        for (CmBsProcess process : processList) {
            CmBsProcessInspection inspection = new CmBsProcessInspection();
            inspection.setApproveStatus("00");
            inspection.setFbfxId(fbfxId);
            inspection.setProcessId(process.getId());
            inspection.setName(process.getName());
            inspection.setSort(process.getSort());
            inspection.setZdId(bo.getZdId());
            processInspections.add(inspection);
            baseMapper.insert(inspection);
            //新增子表数据；
            String fileType = process.getFileType();
            if(StringUtils.isNotBlank(fileType)){
                String[] split = fileType.split(",");
                List<String> fileTypes = new ArrayList<>(Arrays.asList(split));
                List<CmBsProcessInspectionInfo> infos = new ArrayList<>();
                for(String type : fileTypes){
                    CmBsProcessInspectionInfo info = new CmBsProcessInspectionInfo();
                    info.setFileType(type);
                    info.setName(type);
                    info.setProcessInspectionId(inspection.getId());
                    infos.add(info);
                }
                if(CollUtil.isNotEmpty(infos)){
                    inspectionInfoMapper.insertBatch(infos);
                }
            }
        }
        return true;
    }

    /**
     * 工序报验流程发起审批前的校验
     * 1、校验是否上传文件
     * 2、校验如果是前6道工序，是否上一道工序审批完成。
     * @param id
     * @return
     */
    @Override
    public int check(Long id) {
        CmBsProcessInspectionVo inspectionVo = baseMapper.selectVoById(id);
        if (StringUtils.isEmpty(inspectionVo.getFileNo())) {
            List<CmBsProcessInspectionInfo> infos = inspectionInfoMapper.selectList(new LambdaQueryWrapper<CmBsProcessInspectionInfo>().eq(CmBsProcessInspectionInfo::getProcessInspectionId, id));
            int flag = 0;
            for (CmBsProcessInspectionInfo info : infos) {
                if (StringUtils.isBlank(info.getFileNo())) {
                    flag++;
                }
            }
            if (flag == infos.size()) {
                throw new ServiceException("请先上传相关文件");
            }
        }
        Long fbfxId = inspectionVo.getFbfxId();
        LambdaQueryWrapper<CmBsProcessInspection> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CmBsProcessInspection::getFbfxId, fbfxId);
        lqw.eq(CmBsProcessInspection::getDelFlag, "0");
        List<CmBsProcessInspectionVo> inspectionVos = baseMapper.selectVoList(lqw);
        if (CollUtil.isNotEmpty(inspectionVos)) {
            List<CmBsProcessInspectionVo> beforeInspctions = inspectionVos.stream().filter(e -> e.getSort() < inspectionVo.getSort()).collect(Collectors.toList());
            if (beforeInspctions.size() >= 6) {
                return 0;
            }
            for (CmBsProcessInspectionVo vo : beforeInspctions) {
                if (!"22".equals(vo.getApproveStatus())) {
                    throw new ServiceException("前一道工序未审批完成，不允许发起后一道工序");
                }
            }
        }
        return 0;
    }

    /**
     * 获取分部分项工序报验树；
     * @param bo
     * @return
     */
    @Override
    public List<BdTreeVo> getFbfxGxTree(CmBsProcessInspectionBo bo) {

        Long zdId = bo.getZdId();
        Long qsId = bo.getQsId();
        if (zdId == null || qsId == null) {
            return new ArrayList<>();
        }
        List<BaseBsFbfx> baseBsFbfxes = fbfxMapper.selectList(new LambdaQueryWrapper<BaseBsFbfx>().eq(BaseBsFbfx::getZdId, zdId));
        if (CollUtil.isEmpty(baseBsFbfxes)) {
            return new ArrayList<>();
        }
        List<BdTreeVo> treeVos = new ArrayList<>();
        //遍历分部分项
        for (BaseBsFbfx fbfx : baseBsFbfxes) {
            if (!fbfx.getParentId().equals(fbfx.getZdId())) {
                continue;
            }
            BdTreeVo vo = new BdTreeVo();
            vo.setLabel(fbfx.getName());
            vo.setId(fbfx.getId());
            vo.setParentId(null);
            vo.setType("fbfx");
            vo.setChildren(getFbfxGxTreeChildren(fbfx, qsId));
            treeVos.add(vo);
        }
        return treeVos;
    }

    /**
     * 获取工序报验所有附件根据附件id；
     * @param bo
     * @return
     */
    @Override
    public List<SysOssVo> getGxbyFiles(CmBsProcessInspectionBo bo) {
        Set<Long> fileIds = new HashSet<>();
        Long id = bo.getId();
        String fileNo = "";
        CmBsProcessInspectionVo inspectionVo = baseMapper.selectVoById(id);
        if (inspectionVo == null) {
            return new ArrayList<>();
        }
        fileNo = StringUtils.isBlank(inspectionVo.getFileNo()) ? "" : inspectionVo.getFileNo() + ",";
        List<CmBsProcessInspectionInfo> infos = inspectionInfoMapper.selectList(new LambdaQueryWrapper<CmBsProcessInspectionInfo>().eq(CmBsProcessInspectionInfo::getProcessInspectionId, id));
        if (CollUtil.isNotEmpty(infos)) {
            for (CmBsProcessInspectionInfo info : infos) {
                String infoFileNo = info.getFileNo();
                if (StringUtils.isNotBlank(infoFileNo)) {
                    fileNo = fileNo + infoFileNo + ",";
                }
            }
        }
        List<SysOssVo> sysOssVos = new ArrayList<>();
        if (StringUtils.isNotBlank(fileNo) && fileNo.length() > 1) {
            sysOssVos = getfilesByFileIds(fileNo);
        }
        return sysOssVos;
    }

    /**
     * 查询用户关联项目下的所有标段、站点、分部分项列表；
     * @param bo
     * @return
     */
    @Override
    public List<BdTreeVo> getZdFbfxList(CmBsProcessInspectionBo bo) {
        Long userId = LoginHelper.getUserId();
//        SysUser sysUser = userMapper.selectUserById(userId);
        List<SysUserPower> powers = userPowerMapper.selectList(new LambdaQueryWrapper<SysUserPower>().eq(SysUserPower::getUserId,userId));
        if(CollUtil.isEmpty(powers)){
            return new ArrayList<>();
        }
        Set<Long> powerIds = powers.stream().map(SysUserPower::getPowerId).collect(Collectors.toSet());
        if(CollUtil.isEmpty(powerIds)){
            return new ArrayList<>();
        }
        //关联的标段；
        List<BaseBsBdVo> bdVos = bdMapper.selectVoBatchIds(powerIds);
        //关联的站点；
        List<BaseBsZdVo> baseBsZdVos = zdMapper.selectVoBatchIds(powerIds);
        Set<Long> zdIds = baseBsZdVos.stream().map(BaseBsZdVo::getId).collect(Collectors.toSet());
        Map<Long, List<BaseBsZdVo>> bdIdZdListMap = baseBsZdVos.stream().collect(Collectors.groupingBy(BaseBsZdVo::getBdId));
        //根据站点集合查询所有分部分项；
        if(CollUtil.isEmpty(zdIds)){
            return new ArrayList<>();
        }
        List<BaseBsFbfxVo> fbfxes = fbfxMapper.selectVoList(new LambdaQueryWrapper<BaseBsFbfx>().in(BaseBsFbfx::getZdId,zdIds));
        if(CollUtil.isEmpty(fbfxes)){
            return new ArrayList<>();
        }
        Map<Long, List<BaseBsFbfxVo>> zdFbfxList = fbfxes.stream().collect(Collectors.groupingBy(BaseBsFbfxVo::getZdId));
        List<BdTreeVo> result = new ArrayList<>();
        if(CollUtil.isNotEmpty(bdVos)) {
            //遍历标段；
            for (BaseBsBdVo bdVo : bdVos) {
                List<BaseBsZdVo> zdVos = bdIdZdListMap.get(bdVo.getId());
                if (CollUtil.isNotEmpty(zdVos)) {
                    //遍历站点；
                    for (BaseBsZdVo zdVo : zdVos) {
                        //站点下的所有分布分项；
                        List<BaseBsFbfxVo> fbfxVos = zdFbfxList.get(zdVo.getId());
                        if (CollUtil.isNotEmpty(fbfxVos)) {
                            for (BaseBsFbfxVo fbfxVo : fbfxVos) {
                                //只取有工序报验的分部分项，
                                List<CmBsProcessInspectionVo> gxbyList = baseMapper.selectVoList(new LambdaQueryWrapper<CmBsProcessInspection>().eq(CmBsProcessInspection::getFbfxId, fbfxVo.getId()));
                                if (CollUtil.isNotEmpty(gxbyList)) {
                                    BdTreeVo vo = new BdTreeVo();
                                    vo.setId(fbfxVo.getId());
                                    //拼接上标段名、
                                    vo.setLabel(bdVo.getName() + "+" + getFbfxNamepj(fbfxVo.getId(), true));
                                    result.add(vo);
                                }
                            }
                        }
                    }
                }

            }
        }
        //模糊查询
        if(StringUtils.isNotBlank(bo.getName())){
            result = result.stream().filter(e -> e.getLabel().contains(bo.getName())).collect(Collectors.toList());
        }
        return result;
    }

    /**
     * 工序报验查询列表分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<CmBsProcessInspectionVo> queryPageQueryList(CmBsProcessInspectionBo bo, PageQuery pageQuery) {
        // 根据项目id获取标段列表
        BaseBsBdBo bdBo = new BaseBsBdBo();
        bdBo.setProjectId(bo.getProjectId());
        if (bo.getAllProject() == null || "false".equals(bo.getAllProject())) {
            R<List<BaseBsBdVo>> data = bdService.getList(bdBo);
            List<Long> bdIds = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(data.getData())) {
                List<BaseBsBdVo> bdVos = data.getData();
                bdIds = bdVos.stream().map(BaseBsBdVo::getId).collect(Collectors.toList());
                bo.setBdIds(bdIds);
            }
        }

        // 条件查询
        Page<CmBsProcessInspectionVo> result = baseMapper.queryPageQueryList(pageQuery.build(), bo);

        result.getRecords().forEach(item -> {
            // 获取分部分项名称
            if (ObjectUtils.isNotEmpty(item.getFbfxId())) {
                item.setFbfxName(getFbfxNamepj(item.getFbfxId(), false));
            }
            // 获取当前审批节点
            if (ObjectUtils.isNotEmpty(item.getProcInsId())) {
                ProcessQuery pa = new ProcessQuery();
                pa.setProInsId(item.getProcInsId());
                List<WfTaskVo> wfTaskVos = processService.selectTodoProcessListByProInsId(pa);
                item.setWfTaskVos(wfTaskVos);
                // 当前审批节点
                if (CollUtil.isNotEmpty(wfTaskVos)) {
                    item.setCurrNodeName(wfTaskVos.get(0).getAssigneeName());
                }
                // 是否当前审批人
                Task task = taskService.createTaskQuery().processInstanceId(item.getProcInsId()).singleResult();
                if (task != null) {
                    item.setTaskId(task.getId());
                    if (task.getAssignee() != null && (Long.valueOf(task.getAssignee()) == LoginHelper.getUserId())) {
                        item.setCurrApproval(true);
                    } else {
                        item.setCurrApproval(false);
                    }
                }

            }
        });

        return TableDataInfo.build(result);
    }

    //递归获取分部分项工序清单子节点；
    private List<BdTreeVo> getFbfxGxTreeChildren(BaseBsFbfx fbfx,Long qsId) {
        List<BdTreeVo> treeVos = new ArrayList<>();
        //查询子分部分项列表；
        List<BaseBsFbfx> fbfxes = fbfxMapper.selectList(new LambdaQueryWrapper<BaseBsFbfx>().eq(BaseBsFbfx::getParentId, fbfx.getId()));
        if (CollUtil.isNotEmpty(fbfxes)) {
            for (BaseBsFbfx bsFbfx : fbfxes) {
                BdTreeVo vo = new BdTreeVo();
                vo.setLabel(bsFbfx.getName());
                vo.setId(bsFbfx.getId());
                vo.setParentId(fbfx.getId());
                vo.setType("fbfx");
                vo.setChildren(getFbfxGxTreeChildren(bsFbfx,qsId));
                treeVos.add(vo);
            }
        } else {
            List<CmBsProcessInspection> processInspections = baseMapper.selectList(new LambdaQueryWrapper<CmBsProcessInspection>()
                .eq(CmBsProcessInspection::getFbfxId, fbfx.getId())
                .eq(CmBsProcessInspection::getQsId,qsId)
            );
            if (CollUtil.isNotEmpty(processInspections)) {
                for (CmBsProcessInspection inspection : processInspections) {
                    BdTreeVo vo = new BdTreeVo();
                    vo.setChildren(new ArrayList<>());
                    vo.setId(inspection.getId());
                    vo.setLabel(inspection.getName());
                    vo.setParentId(fbfx.getId());
                    vo.setType("gxby");
                    treeVos.add(vo);
                }
            }
        }
        return treeVos;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmBsProcessInspection entity){
        //TODO 做一些数据校验,如唯一约束
    }

}
